package com.yanqu.road.server.manager.activity.dragonboatrace;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.union.UnionBaseInfo;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.pb.dragonboatrace.DragonBoatRaceProto;
import com.yanqu.road.server.CrossBaseServer;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.date.DateHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description
 * @Author cwq
 * @Data 2022/2/15 15:54
 */
public class DragonBoatRaceMgr extends TempMgr {

    public static Map<Integer, DragonBoatRaceActivity> activityMap = new ConcurrentHashMap<>();

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        // 加载配置
        DragonBoatRaceConfigMgr.reloadConfig();
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        // 初始化 活动
        synchronized (this) {
            String nowTimeStr = DateHelper.getCurrentDateTimeString();
            List<Integer> activityTypeList = new ArrayList<>();
            activityTypeList.add(eActivityType.DragonBoatRaceActivity.getValue());
            Map<Integer, ActivityInfo> activityInfoMap = ActivityBussiness.getOpenCrossRankActivityInfoMapByCrossServerId(activityTypeList, nowTimeStr, CrossBaseServer.getInstance().getServerId());

            for (ActivityInfo activityInfo : activityInfoMap.values()) {
                getDragonBoatRaceActivity(activityInfo.getActivityId());
            }
        }
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    @Override
    public boolean save() {
        for (DragonBoatRaceActivity item : activityMap.values()) {
            item.save();
        }
        return true;
    }

    /**
     * @param activityId
     * @return
     */
    public static synchronized DragonBoatRaceActivity getDragonBoatRaceActivity(int activityId) {
        DragonBoatRaceActivity dragonBoatRaceActivity = activityMap.get(activityId);
        if (dragonBoatRaceActivity == null) {
            dragonBoatRaceActivity = new DragonBoatRaceActivity(activityId);
            activityMap.put(activityId, dragonBoatRaceActivity);
        }
        return dragonBoatRaceActivity;
    }


    /**
     * 联盟增加航行记录
     *
     * @param activityId
     * @param userId
     */
    public static void addDragonBoatRaceVoyageRecord(int activityId, String unionId, long userId, long distance, long cutTime, UnionBaseInfo unionBase) {
        DragonBoatRaceActivity dragonBoatRaceActivity = getDragonBoatRaceActivity(activityId);
        if (dragonBoatRaceActivity == null) {
            return;
        }
        synchronized (dragonBoatRaceActivity) {
            dragonBoatRaceActivity.addDragonBoatRaceVoyageRecord(unionId, userId, distance, cutTime, unionBase);
        }
    }

    /**
     * 进入地图
     */
    public static DragonBoatRaceProto.DragonBoatRaceEnterMapRespMsg.Builder enterMap(int activityId, String unionId, int placeId) {
        DragonBoatRaceProto.DragonBoatRaceEnterMapRespMsg.Builder builder = DragonBoatRaceProto.DragonBoatRaceEnterMapRespMsg.newBuilder();
        DragonBoatRaceActivity dragonBoatRaceActivity = getDragonBoatRaceActivity(activityId);
        if (dragonBoatRaceActivity == null) {
            builder.setRet(GameErrorCode.E_PAGEANT_ACTIVITY_NOT_FOUND);
            return builder;
        }
        return dragonBoatRaceActivity.enterMap(unionId, placeId);
    }

    /**
     * 查看领取详情
     *
     * @param activityId
     * @param unionId
     * @param placeId
     * @return
     */
    public static DragonBoatRaceProto.DragonBoatRaceGetReceivedRecordRespMsg.Builder getPlaceReceived(int activityId, String unionId, int placeId) {
        DragonBoatRaceProto.DragonBoatRaceGetReceivedRecordRespMsg.Builder builder = DragonBoatRaceProto.DragonBoatRaceGetReceivedRecordRespMsg.newBuilder();
        DragonBoatRaceActivity dragonBoatRaceActivity = getDragonBoatRaceActivity(activityId);
        if (dragonBoatRaceActivity == null) {
            builder.setRet(GameErrorCode.E_PAGEANT_ACTIVITY_NOT_FOUND);
            return builder;
        }
        return dragonBoatRaceActivity.getPlaceReceived(unionId, placeId);
    }

    /**
     * 赛龙舟查看档位领取详情
     *
     * @param activityId
     * @param unionId
     * @return
     */
    public static DragonBoatRaceProto.DragonBoatRaceGetUnionRecordRespMsg.Builder getUnionRecord(int activityId, String unionId) {
        DragonBoatRaceProto.DragonBoatRaceGetUnionRecordRespMsg.Builder builder = DragonBoatRaceProto.DragonBoatRaceGetUnionRecordRespMsg.newBuilder();
        DragonBoatRaceActivity dragonBoatRaceActivity = getDragonBoatRaceActivity(activityId);
        if (dragonBoatRaceActivity == null) {
            builder.setRet(GameErrorCode.E_PAGEANT_ACTIVITY_NOT_FOUND);
            return builder;
        }
        return dragonBoatRaceActivity.getUnionRecord(unionId);
    }

    /**
     * 赛龙舟查看航行记录
     *
     * @param activityId
     * @param unionId
     * @return
     */
    public static DragonBoatRaceProto.DragonBoatRaceGetVoyageRecordRespMsg.Builder getVoyageRecord(int activityId, String unionId) {
        DragonBoatRaceProto.DragonBoatRaceGetVoyageRecordRespMsg.Builder builder = DragonBoatRaceProto.DragonBoatRaceGetVoyageRecordRespMsg.newBuilder();
        DragonBoatRaceActivity dragonBoatRaceActivity = getDragonBoatRaceActivity(activityId);
        if (dragonBoatRaceActivity == null) {
            builder.setRet(GameErrorCode.E_PAGEANT_ACTIVITY_NOT_FOUND);
            return builder;
        }
        return dragonBoatRaceActivity.getVoyageRecord(unionId);
    }

    /**
     * 领取奖励
     *
     * @param activityId
     * @param unionId
     * @param placeId
     */
    public static DragonBoatRaceProto.DragonBoatRaceReceiveRecordRewardCrossToGameMsg.Builder receiveRecord(int activityId, String unionId, long userId, int placeId) {
        DragonBoatRaceProto.DragonBoatRaceReceiveRecordRewardCrossToGameMsg.Builder crossToGameMsg = DragonBoatRaceProto.DragonBoatRaceReceiveRecordRewardCrossToGameMsg.newBuilder();
        crossToGameMsg.setPlaceId(placeId);
        DragonBoatRaceProto.DragonBoatRaceReceiveRecordRewardRespMsg.Builder stcMsg = DragonBoatRaceProto.DragonBoatRaceReceiveRecordRewardRespMsg.newBuilder();
        DragonBoatRaceActivity dragonBoatRaceActivity = getDragonBoatRaceActivity(activityId);
        if (dragonBoatRaceActivity == null) {
            stcMsg.setRet(GameErrorCode.E_PAGEANT_ACTIVITY_NOT_FOUND);
            crossToGameMsg.setStcMsg(stcMsg);
            return crossToGameMsg;
        }
        stcMsg = dragonBoatRaceActivity.receiveRecord(unionId, userId, placeId);
        crossToGameMsg.setStcMsg(stcMsg);
        if (stcMsg.getRet() == 0) {
            crossToGameMsg.setSynUnionInfo(dragonBoatRaceActivity.getDistanceAndReceivedInfo(unionId));
        }
        return crossToGameMsg;
    }


    /**
     * 获取联盟航行距离和领取信息
     *
     * @param activityId
     * @param unionId
     * @return
     */
    public static void getDistanceAndReceivedInfo(long userId, long serverId, int activityId, String unionId) {
        DragonBoatRaceActivity dragonBoatRaceActivity = getDragonBoatRaceActivity(activityId);
        if (dragonBoatRaceActivity == null) {
            return;
        }
        DragonBoatRaceProto.DragonBoatRaceSynUnionInfoCrossToGameMsg.Builder builder = dragonBoatRaceActivity.getDistanceAndReceivedInfo(unionId);
        if (builder == null) {
            return;
        }
        builder.setType(1);
        MessageHelper.sendPacket(serverId, userId, YanQuMessageUtils.buildMessage(GameProtocol.S_DRAGONBOATRACE_DISTANCE_AND_RECEIVED_INFO_FROM_CROSS, builder));
    }

    /**
     * 同步联盟信息
     */
    public static void synUnionInfo(int activityId, String unionId, int type, UnionBaseInfo unionBase) {
        DragonBoatRaceActivity dragonBoatRaceActivity = getDragonBoatRaceActivity(activityId);
        if (dragonBoatRaceActivity == null) {
            return;
        }
        dragonBoatRaceActivity.synUnionInfo(unionId, type, unionBase);
    }

}
